package com.javatip.common.utils;

import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class BatchResizeImages {

    public static void main(String[] args) {
        String directoryPath = "/home/jolly/develop/project/java/TrainingManager/src/main/resources/datasets/1/袁伟";
        int targetWidth = 256;
        int targetHeight = 256;

        resizeImagesInDirectory(directoryPath, targetWidth, targetHeight);
    }

    /**
     * 批量调整指定目录下所有图片的尺寸。
     * @param directoryPath 目录路径
     * @param targetWidth 目标宽度
     * @param targetHeight 目标高度
     */
    public static void resizeImagesInDirectory(String directoryPath, int targetWidth, int targetHeight) {
        File directory = new File(directoryPath);

        // 检查目录是否存在
        if (!directory.exists() || !directory.isDirectory()) {
            System.out.println("无效的目录路径: " + directoryPath);
            return;
        }

        // 列出目录中的所有文件
        File[] files = directory.listFiles();

        if (files == null) {
            System.out.println("目录中没有找到文件: " + directoryPath);
            return;
        }

        // 处理每个文件
        for (File file : files) {
            if (file.isFile()) {
                String fileName = file.getName().toLowerCase();
                if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg") || fileName.endsWith(".png")) {
                    resizeImage(file.getAbsolutePath(), targetWidth, targetHeight);
                }
            } else if (file.isDirectory()) {
                // 递归处理子目录
                resizeImagesInDirectory(file.getAbsolutePath(), targetWidth, targetHeight);
            }
        }

        System.out.println("批量调整大小完成.");
    }

    /**
     * 调整指定图片的尺寸，不改变原始图片的大小，而是在目标尺寸上直接添加像素，超过目标尺寸的部分进行裁剪。
     * @param inputImagePath 输入图片路径
     * @param targetWidth 目标宽度
     * @param targetHeight 目标高度
     */
    public static void resizeImage(String inputImagePath, int targetWidth, int targetHeight) {
        try {
            // 读取输入图片
            File inputFile = new File(inputImagePath);
            BufferedImage inputImage = ImageIO.read(inputFile);

            // 创建输出图片
            BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, inputImage.getType());
            Graphics2D g2d = outputImage.createGraphics();

            // 计算缩放比例和裁剪位置
            double ratio = (double) inputImage.getWidth() / inputImage.getHeight();
            int scaledWidth = targetWidth;
            int scaledHeight = targetHeight;
            int xStart = 0;
            int yStart = 0;

            if (ratio > 1) {
                scaledHeight = (int) Math.round(targetWidth / ratio);
                yStart = (targetHeight - scaledHeight) / 2;
            } else {
                scaledWidth = (int) Math.round(targetHeight * ratio);
                xStart = (targetWidth - scaledWidth) / 2;
            }

            // 绘制原始图片到输出图片
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2d.drawImage(inputImage, xStart, yStart, scaledWidth, scaledHeight, null);
            g2d.dispose();

            // 写入输出图片（覆盖原始图片）
            ImageIO.write(outputImage, "jpg", inputFile);

            System.out.println("调整大小后的图片已保存: " + inputImagePath);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
